package it.prova.drop;

import it.prova.Consts;
import it.prova.MyVariable;
import it.prova.dialog.SingleStringDialog;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.itemcreation.customizer.AbstractDropCustomizer;

@SuppressWarnings("restriction")
public class IterateDropCustomizer extends AbstractDropCustomizer
{
	private MyVariable dropCustomizationData;
	
//	private Document currentModel;
	
	@SuppressWarnings("unused")
	private String tagName;
	
	public IterateDropCustomizer(String tagName)
	{
		super();
		
		this.tagName = tagName;
	}
	
	@Override
	public IAdaptable getDropCustomizationData()
	{
		return dropCustomizationData;
	}

	/*
	 *  from JavaDoc: "This is deprecated and is no longer called directly."
	 */
	@Override
	public IStatus runCustomizer()
	{
		return Status.OK_STATUS;
	}

	@Override
	public IStatus runCustomizer(IFile file, IDOMPosition position)
	{	
//		currentModel = Utility.getCurrentModel();
		
		SingleStringDialog dialog = new SingleStringDialog
		(
			null, 
			"Valore", 
			null
		);
		
		int result = dialog.open();
		System.out.println("result = " + result);			
		String selectedAttributeValue = dialog.getSelectedAttributeValue();
		System.out.println("dialog.getSelectedAttributeValue() = " + selectedAttributeValue);
		
//		// write received code to .model file
//		if
//		(
//			(result == Status.OK)
//		)
//		{
//			System.out.println("saving...");
//			
//			Element attribute = Utility.getAttributeElement(currentModel, selectedAttributeValue);
//			if(attribute != null)
//			{
//				Utility.manageElement(currentModel, attribute, selectedAttributeContent, tagName);
//				System.out.println("currentModel.toXml(): " + currentModel.toXML());
//				
//				Utility.setCurrentModel(currentModel);
//			}
//			else
//			{
//				System.out.println("Cannot find a tag with '" + selectedAttributeValue + "' value for 'name' attribute inside current model");
//			}
//		} 
//		
		if(result != Status.OK)
		{
			return Status.CANCEL_STATUS;
		}
		else
		{
			dropCustomizationData = new MyVariable(Consts.TAG_ATTRIBUTE_VALUE, String.valueOf(selectedAttributeValue));
			
			return Status.OK_STATUS;
		}
	}
}